#ifndef __PEERSIMULATOR__
#define __PEERSIMULATOR__

#include <vector>
#include "VectorTimer.h"
#include "Updater.h"
#include <string>
#include "DetermineUpdate.h"
#include "HelperClass.h"
#include <stdio.h>
#include <stdlib.h>

class PeerSimulator //: public Updater
{
private:
	FixedVectorTimer<std::string>& GetTempVector(std::string& newUpdate)
	{
		int test = 0;
		FixedVectorTimer<std::string> testTempUpdate(test);
		testTempUpdate.FromString(newUpdate);
		return testTempUpdate;
	}

public:
	FixedVectorTimer<std::string> updater;
	int peerIndex;

	PeerSimulator()
	{
		int defaultIndex = 0;
		updater = FixedVectorTimer<std::string>(defaultIndex);
		InitTimers();
	}

	PeerSimulator(std::string& m_assignedLetter, int& m_peerIndex)
	{
		updater = FixedVectorTimer<std::string>(m_peerIndex);
		peerIndex = m_peerIndex;

		updater.updateRepresented = m_assignedLetter;
		
	}

	PeerSimulator(int& m_peerIndex, std::string& data)
	{
		updater = FixedVectorTimer<std::string>(m_peerIndex);
		updater.FromString(data);
		peerIndex = m_peerIndex;
	}

	UpdateRelathionship MergeUpdates(FixedVectorTimer<std::string>& lhs, FixedVectorTimer<std::string>& rhs)
	{
		UpdateRelathionship outcome = DetermineUpdate<std::string>::DetermineUpdatesSequence(lhs, rhs);
		return outcome;
	}

	//using Updater::ParseUpdates;
	virtual std::string ParseUpdates()
	{
		return updater.ToString();
	}

	void InitTimers()
	{
		//no initialization needed
	}
	
	//using Updater::MergeUpdates;
	void MergeUpdates(FixedVectorTimer<std::string>& recivedUpdate)
	{
		///////////////////////check here
		UpdateRelathionship outcome = DetermineUpdate<std::string>::DetermineUpdatesSequence(updater, recivedUpdate);

		switch (outcome)
		{
		case UPDATES_CONCURRENT:
			
			for (int index = 0; index < recivedUpdate.vectorTimer.size(); index++)
			{
				updater.vectorTimer[index] = updater.vectorTimer[index] < recivedUpdate.vectorTimer[index] ? recivedUpdate.vectorTimer[index] : updater.vectorTimer[index];
			}
			
			updater.updateRepresented = updater.updateRepresented + recivedUpdate.updateRepresented;
			break;

		case RHS_LATEST:
			updater = recivedUpdate;
			break;

		case STARING:
			//equal updates
			break;

		default:
			break;
		}
	}



	void UseRecivedUpdates(std::string& update)
	{
		MergeUpdates(GetTempVector(update));
	}
};


#endif // !__PEERSIMULATOR__
